package com.xyoto.community.listener;

import com.xyoto.community.cache.CacheInstruction;
import com.xyoto.community.constant.PictureConstant;
import com.xyoto.community.mapper.picture.PictureMapper;
import com.xyoto.community.po.PictureListPO;
import com.xyoto.community.service.PictureService;
import com.xyoto.community.service.impl.PictureServiceImpl;
import com.xyoto.community.utils.RedisCommonUtils;
import com.xyoto.community.utils.XYOtoOss;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author coderFu
 * @create 2021/10/20
 */
@ApiModel("redis key 过期监听器")
@Component
@Slf4j
@Data
public class RedisExpiredKeyListener implements MessageListener{



    private RedisCommonUtils redisCommonUtils;


    private PictureService pictureService;


    private PictureMapper pictureMapper;


    private XYOtoOss xyOtoOss;


    /**
     * 监听过期的key
     * @param message
     * @param bytes
     */
    @Override
    public void onMessage(@NotNull Message message, byte[] bytes) {
        //获取过期的key值
        byte[] body = message.getBody();
        String expireKey = new String(body);
        //大于32 判断是否为需要的key ==》图片key
        if (expireKey.length() > PictureConstant.EXPIRE_KEY_LENGTH){
            List pictureIds = (List)redisCommonUtils.hget(PictureConstant.PICTURE_ID_CACHE_HASH_NAME, expireKey);
            if (pictureIds != null && pictureIds.size() >0){
                log.info("该key为有效的图片key");
                //切割出表名
                String tableName = expireKey.split(",")[1];
                //查询所有的图片路径
               List<PictureListPO>  pictureList = pictureMapper.selectListByPictureIds(tableName,pictureIds);
               if (pictureList.size() > 0){
                   //删除oss中的图片信息
                   for (PictureListPO pictureListPo : pictureList) {
                       xyOtoOss.deleteFileAvatar(pictureListPo.getPath());
                       //删除存储的图片详情缓存
                       redisCommonUtils.hdel(CacheInstruction.DYNAMIC_PICTURE,pictureListPo.getId());
                   }
                   log.info("【删除成功】，oss中的图片信息删除成功");
                   //删除数据库中的记录
                   pictureMapper.deleteBatchPictureByPictureIds(tableName,pictureIds);
                   log.info("【删除成功】,数据库中的记录删除成功");
                   //删除备份的key
                   redisCommonUtils.hdel(PictureConstant.PICTURE_ID_CACHE_HASH_NAME,expireKey);
                   log.info("【删除成功】，删除备份的key成功");

               }
            }
        }
        log.info("【过期的key】为"+expireKey);
    }
}
